/*
 * Copyright 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.beepmystuff;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;

import com.google.beepmystuff.NetworkService.NetworkServiceConnection;

/**
 * Shows detailed information about a single item in the scan registry.
 */
public class ItemDetailsActivity extends ListActivity {
  private static final String TAG = "BMS.ItemDetailsActivity";
  private NetworkServiceConnection networkServiceConnection = new NetworkServiceConnection();
  private NetworkService networkService;

  /**
   * Called when the activity is first created.
   * */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.item_details);

    // Establish a connection with our service (starting it if necessary).
    NetworkService.bind(this, networkServiceConnection,
        new Runnable() {
          @Override
          public void run() {
            networkService = networkServiceConnection.getService();
            createAfterServiceAvailable();
          }
    });
  }

  /** Continue creation after the network service has been started. */
  private void createAfterServiceAvailable() {
    // Create the list view adapter which takes information from the
    // ScanRegistry and binds it to our view.
    ScanRegistry scanRegistry = networkService.getScanRegistry();

    // Add a listener to the scan registry, listening for change events in the
    // data, and causing the list view to refresh. Note that the events come in
    // from a separate thread but the adapter needs to be queried in the UI
    // thread.
    scanRegistry.addListener(new ScanRegistry.Listener() {
      public void notifyChanged() {
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            Log.d(TAG, "Data changed, requerying");
            //adapter.getCursor().requery();
          }
        });
      }
    });
  }

  /**
   * Called before the activity is destroyed.
   */
  @Override
  public void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "onDestroy");
    // This check isn't foolproof: if the service has been recently unbound,
    // we may not have received the notification yet, but this call will still
    // fail.  Luckily, we're the only people who unbind the service, so we
    // should be okay.
    if (networkServiceConnection.getService() != null) {
      unbindService(networkServiceConnection);
    }
  }
}
